package com.zabra.zl.ui.forgetpwd;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.forgetpwd.contract.ForgetPwdContract;
import com.zabra.zl.ui.forgetpwd.presenter.ForgetPwdPresenter;
import com.zabra.zl.ui.updatepwd.UpdatePwdActivity;
import com.zabra.zl.utils.CountDownTimerUtils;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPwdActivity extends BaseMvpActivity<ForgetPwdPresenter> implements ForgetPwdContract.View {


    @BindView(R.id.et_tel)
    EditText etTel;
    @BindView(R.id.vercode)
    EditText vercode;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.bt_next)
    TextView btNext;
    @BindView(R.id.bt_login)
    TextView btLogin;
    private String strTel, strCode;
    private Dialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    public void initView() {
        mPresenter = new ForgetPwdPresenter();
        mPresenter.attachView(this);
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(getCode, 60000, 1000);
            mCountDownTimerUtils.start();
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void onSuccessData(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            Bundle bundle=new Bundle();
            bundle.putString("tel",strTel);
            toClass(this, UpdatePwdActivity.class,bundle);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @OnClick({R.id.get_code, R.id.bt_next, R.id.bt_login})
    public void onViewClicked(View view) {
        strTel = etTel.getText().toString().trim();
        switch (view.getId()) {
            case R.id.get_code:
                if (TextUtils.isEmpty(strTel)) {
                    ToastUtil.showToast(this, "请输入手机号码");
                } else {
                    keyList.add("phone");
                    valueList.add(strTel);
                    mPresenter.getCode(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }
                break;
            case R.id.bt_next:
                strCode = vercode.getText().toString().trim();
                if (TextUtils.isEmpty(strTel) || TextUtils.isEmpty(strCode)) {
                    ToastUtil.showToast(this, "请输入验证码");
                } else {
                    keyList.add("phone");
                    keyList.add("yzm");
                    valueList.add(strTel);
                    valueList.add(strCode);
                    mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }
                break;
            case R.id.bt_login:
                finish();
                break;
        }
    }
}
